﻿using Microsoft.AspNetCore.Mvc;
using StarsAdmin.Application.File.Dtos;
using StarsAdmin.Application.File.Services;
using StarsAdmin.Core.Attributes;
using StarsAdmin.Core.Models;
using StarsAdmin.Web.Core.Conttrollers;
using System.ComponentModel.DataAnnotations;

namespace StarsAdmin.WebApi.Conttrollers
{
    /// <summary>
    /// 文件管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Order(8)]
    public class FileController : BaseController
    {
        private readonly IFileService _fileService;

        public FileController(IFileService fileService)
        {
            _fileService = fileService;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<FileDto>>> GetAllAsync([FromQuery] BaseQueryInput input)
        {
            return (await _fileService.GetAllAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<Page<FileDto>>> GetPageAsync([FromQuery] PageQueryInput input)
        {
            return (await _fileService.GetPageAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<FileDto>> GetByIdAsync([Required] long id)
        {
            return (await _fileService.GetByIdAsync(id)).ResultFormat("没有找到该条数据", 404);
        }

        /// <summary>
        /// 根据Id集合获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<FileDto>>> GetByIdsAsync([FromQuery] BaseIdsInput input)
        {
            return (await _fileService.GetByIdsAsync(input.Ids)).ResultFormat();
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<FileDto>> UploadFileAsync([Required] IFormFile file)
        {
            return (await _fileService.UploadFileAsync(file)).ResultFormat("文件上传失败");
        }

        /// <summary>
        /// 批量上传
        /// </summary>
        /// <param name="formFiles"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<List<FileDto>>> UploadFilesAsync([Required] IFormFileCollection formFiles)
        {
            return (await _fileService.UploadFilesAsync(formFiles)).ResultFormat("文件上传失败");
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DownloadFileAsync(DownloadFileInput input)
        {
            var result = await _fileService.DownloadFileAsync(input);
            return result != null ? result : NotFound();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> DeleteAsync([Required] long id)
        {
            return (await _fileService.DeleteAsync(id)).ResultFormat("删除失败");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> BatchDeleteAsync(BaseIdsInput input)
        {
            return (await _fileService.BatchDeleteAsync(input.Ids)).ResultFormat("批量删除失败");
        }
    }
}